package com.a3dweijing.wetouch2.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.a3dweijing.wetouch2.Activity.PartitionTypeAvtivity;
import com.a3dweijing.wetouch2.Activity.ThreeDActivity;
import com.a3dweijing.wetouch2.Activity.VideoPlayActivity;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.PartitionAdapter;
import com.a3dweijing.wetouch2.bean.ExpoonVideo;
import com.a3dweijing.wetouch2.bean.PartitionMsg;
import com.a3dweijing.wetouch2.bean.VideoType;
import com.a3dweijing.wetouch2.callback.PartitionOnclick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2018/3/20.
 * 3d首页
 */

public class FragmentOf3D extends Fragment implements PartitionOnclick {
    private RecyclerView partitionRec;
    private PartitionAdapter adapter;
    private Handler handler;
    private List<VideoType> videoTypes;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view != null) {
            return view;
        }
        view = inflater.inflate(R.layout.fragment_of_3d, container, false);
        partitionRec = (RecyclerView) view.findViewById(R.id.partition_rec);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 100:
                        PartitionMsg partitionMsg = new Gson().fromJson(msg.obj.toString(), PartitionMsg.class);
                        if (partitionMsg != null && partitionMsg.getRecommendList() != null && partitionMsg.getRecommendList().size() > 0) {
                            videoTypes = partitionMsg.getVideoTypeList();
                            partitionRec.setLayoutManager(new LinearLayoutManager(getContext()));
                            adapter = new PartitionAdapter(getContext(), partitionMsg);
                            adapter.setOnclick(FragmentOf3D.this);
                            partitionRec.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                        break;
                    case 404:
                        Log.e("--", msg.obj.toString());
                        break;
                }
            }
        };
        new GetPartitionThread().start();
        return view;
    }

    public static FragmentOf3D newInstance() {
        Bundle bundle = new Bundle();
        bundle.putString("name", "partition");
        FragmentOf3D homeFragment = new FragmentOf3D();
        homeFragment.setArguments(bundle);
        return homeFragment;
    }

    @Override
    public void typeclick(int position, VideoType type) {
        Log.e("--", position + "----" + type);
        if (type.getWc_type_id().equals("12")){
            Intent in = new Intent(getContext(), ThreeDActivity.class);
            startActivity(in);
        }else {
            Intent in = new Intent(getContext(), PartitionTypeAvtivity.class);
            in.putExtra("position", position);
            in.putExtra("types", (Serializable) videoTypes);
            startActivity(in);
        }
    }

    @Override
    public void videoclick(int position, Object o) {
        Intent in = new Intent(getContext(), VideoPlayActivity.class);
        ExpoonVideo video = (ExpoonVideo) o;
        in.putExtra("video", video);
        startActivity(in);
    }

    private class GetPartitionThread extends Thread {
        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetPartitionData();
            Log.e("--", resulet);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            } else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }
}